package pide.core;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import pide.gui.FileView;
import pide.gui.Toolbar;
import debug.DebugMessage;


public class Core {

	private String FILE_CHOOSER_WORKSPACE_DIALOG_LABEL = "Select Workspace..";
	
	public String WORKSPACE="."; 
	
	public Set<String> opened_File = new HashSet<>();
	
	private static Core instance;
	public static Core getDefault()
	{
		if(instance==null)
			instance=new Core();
		return instance;
	}
	
	private Core()
	{
		DebugMessage.debug("Running...");
		
		//set LookAndFeel
		setLookAndFeel();
		
		//set workspace
		Configuration conf = Configuration.getInstance();
		WORKSPACE=conf.get("ide.workspace");
		
		if(WORKSPACE.equals("null"))
			changeWorkspace();		
		
		//run toolbar		
		new Toolbar();
		//run untitled in workspace
		
				
		
		
	}

	public void changeWorkspace() {
		DebugMessage.debug("Workspace choose...");
		JFileChooser jfc = new JFileChooser(WORKSPACE);
		jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);			
		
		int output = jfc.showDialog(new JFrame(), FILE_CHOOSER_WORKSPACE_DIALOG_LABEL);
		if(output==JFileChooser.APPROVE_OPTION)
		{
			WORKSPACE = jfc.getSelectedFile().getAbsolutePath();
			Configuration.getInstance().add("ide.workspace", WORKSPACE);
			DebugMessage.debug("Workspace : "+WORKSPACE);
		}
		else
		{
			DebugMessage.info("Terminated by user");
		}
	}
	
	public void createFile()
	{
		boolean compile=true;
		while(compile)
		{
			
			String obj = JOptionPane.showInputDialog("File name: ");
			if(obj==null) return;
			
			if(obj.isEmpty())
			{
				JOptionPane.showMessageDialog(null, "File name is empty!");
			}
			else
			{		
				File f = new File(WORKSPACE+"/"+obj+".pl");
				if(f.exists())
				{
					JOptionPane.showMessageDialog(null, f.getAbsolutePath()+" already exist!");
				}
				else
				{
					open(f);
					compile =false;
				}
			}
		}
	}
	
	public static void main(String[] args) {
		
		Core.getDefault();
		
//		Enumeration<Object> obj = UIManager.getLookAndFeelDefaults().keys();
//		while(obj.hasMoreElements())
//		{
//			System.out.println(obj.nextElement());
//		}

	}
	
	private void setLookAndFeel()
	{
		DebugMessage.debug("****** LOOK AND FEEL SETTINGS ******");
		DebugMessage.debug("Current : "+UIManager.getLookAndFeel());
		for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
			if("Nimbus".equals(info.getName()))
			{
				try {
					UIManager.setLookAndFeel(info.getClassName());
					DebugMessage.debug("Look and feel changed to "+info);
					
				} catch (Exception e) {					
					e.printStackTrace();
				}
				return;
			}
		}
	}

	public void open(File selectedFile) {
		
		
		if(opened_File.add(selectedFile.getAbsolutePath()))
		{
			new FileView(selectedFile);			
			DebugMessage.debug("closeFileWiew [ "+new ArrayList<>(opened_File)+" ]");
		}
		else
		{
			JOptionPane.showMessageDialog(null, "File already opened!");
		}
		
		
	}
	
	public void closeFileWiew(File selectedFile )
	{
		DebugMessage.debug("closeFileWiew [ "+selectedFile.getAbsolutePath()+" ]");
		opened_File.remove(selectedFile.getAbsolutePath());
	}
	
	
	public void exit() {
		
		if(!opened_File.isEmpty())
		{
			int res = JOptionPane.showConfirmDialog(new JFrame(), "There are some files opened. Discard all change?");
			if(res==JOptionPane.OK_OPTION)
			{				
				DebugMessage.info("Terminated by the user");
				System.exit(1);
			}
		}
		else
		{
			int res = JOptionPane.showConfirmDialog(new JFrame(), "Are you sure?");
			if(res==JOptionPane.OK_OPTION)
			{				
				DebugMessage.info("Terminated by the user");
				System.exit(1);
			}
		}
		
		
	}
	

	public void run(String file) {
		DebugMessage.debug("Run prolog.. ");
		PrologInterface.getDefault().run(file);			
	}
	
	
	
	

}
